package com.utils.basejava.NIO;



import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;
import java.nio.channels.Pipe.SinkChannel;
import java.nio.channels.Pipe.SourceChannel;

public class TestPipe {
	
	
	public void test1() throws IOException {
		//1、获取通道
		Pipe pipe = Pipe.open();
		//2、将缓冲区中的数据写入管道
		ByteBuffer bb = ByteBuffer.allocate(1024);
		
		SinkChannel sink = pipe.sink();
		bb.put("通过单向管道发送数据".getBytes());
		bb.flip();
		sink.write(bb);
		
		//3、读取缓冲区中的数据
		SourceChannel source = pipe.source();
		bb.flip();
		int len = source.read(bb);
//		System.out.println(new String(bb.array(), 0, bb.limit()));
		System.out.println(new String(bb.array(), 0, len));
		
		source.close();
		sink.close();
	}

}
